package sdfs.util;


import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.ProtostuffIOUtil;
import com.dyuproject.protostuff.Schema;
import com.dyuproject.protostuff.runtime.RuntimeSchema;

public class ProtoStuffUtil {
    public static  byte[]  serializeProtoStuffObject(Object obj,Class clazz) {
        if(obj == null) {
            return null;
        }
        Schema schema = RuntimeSchema.getSchema(clazz);
        LinkedBuffer buffer = LinkedBuffer.allocate(4096);
        byte[] protostuff = null;
        protostuff = ProtostuffIOUtil.toByteArray(obj, schema, buffer);
        return protostuff;
    }

    public static Object  deserializeProtoStuffDataListToProductsObject(
            byte[]  bytes,Class clazz) {
        if(bytes == null) {
            return null;
        }
        Schema  schema = RuntimeSchema.getSchema(clazz);
        Object obj = null;
        try {
            obj = clazz.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ProtostuffIOUtil.mergeFrom(bytes,obj, schema);
        return obj;
    }
}
